What is timers-ext?
The timers-ext npm package provides extended timer functionalities for JavaScript, including delay, debounce, throttle, and more. It is useful for managing and controlling the execution of functions over time.
What are timers-ext's main functionalities?
delay
The delay function pauses the execution of the code for a specified amount of time. In this example, the code waits for 2 seconds before logging 'End'.
const delay = require('timers-ext/delay');
async function example() {
console.log('Start');
await delay(2000); // Delay for 2 seconds
console.log('End');
}
example();
debounce
The debounce function ensures that a function is only called once after a specified delay period has passed since the last time it was invoked. This is useful for events like window resizing.
const debounce = require('timers-ext/debounce');
function onResize() {
console.log('Resized');
}
const debouncedResize = debounce(onResize, 300);
window.addEventListener('resize', debouncedResize);
throttle
The throttle function ensures that a function is called at most once in a specified time period. This is useful for events like scrolling where you want to limit the number of times a function is called.
const throttle = require('timers-ext/throttle');
function onScroll() {
console.log('Scrolled');
}
const throttledScroll = throttle(onScroll, 1000);
window.addEventListener('scroll', throttledScroll);
Other packages similar to timers-ext
lodash
Lodash is a popular utility library that provides a wide range of functions for common programming tasks, including debounce and throttle. It is more comprehensive than timers-ext and includes many other utilities for working with arrays, objects, and more.
underscore
Underscore is another utility library similar to Lodash that provides a variety of functional programming helpers, including debounce and throttle. It is less feature-rich than Lodash but still widely used for its simplicity and ease of use.
async
The async library provides powerful functions for working with asynchronous JavaScript, including control flow, collections, and more. While it does not focus specifically on timers, it offers various utilities for managing asynchronous operations.
timers-ext
Timers extensions
Installation
$ npm install timers-ext
To port it to Browser or any other (non CJS) environment, use your favorite CJS bundler. No favorite yet? Try: Browserify, Webmake or Webpack
API
MAX*TIMEOUT *(timers-ext/max-timeout)_
Maximum possible timeout value in milliseconds. It equals to maximum positive value for 32bit signed integer, so 2³¹ (2147483647), which makes it around 24.9 days
delay(fn[, timeout]) (timers-ext/delay)
Returns function which when invoked will call fn function after specified
timeout. If timeout is not provided nextTick propagation is used.
once(fn[, timeout]) (timers-ext/once)
Makes sure to execute fn function only once after a defined interval of time (debounce). If timeout is not provided nextTick propagation is used.
var nextTick = require("next-tick");
var logFoo = function () { console.log("foo"); };
var logFooOnce = require("timers-ext/once")(logFoo);
logFooOnce();
logFooOnce();
logFooOnce();
nextTick(function () {
logFooOnce();
logFooOnce();
logFooOnce();
});
validTimeout(timeout) (timers-ext/valid-timeout)
Validates timeout value.
For NaN
resolved timeout 0
is returned.
If timeout resolves to a number:
- for timeout < 0
0
is returned - for 0 >= timeout <= MAX_TIMEOUT,
timeout
value is returned - for timeout > MAX_TIMEOUT exception is thrown
Tests
$ npm test
Security contact information
To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.